//
//  HBInvestFailedCtrl.m
//  HuoBao
//
//  Created by sky on 15/12/16.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBInvestFailedCtrl.h"
#import "HBMyBankAccountCtrl.h"
#import "HBInvestCtrl.h"
#import "HBBuyingRecordCtrl.h"

@interface HBInvestFailedCtrl (){
    UIImageView *_bankvi;
    UILabel *_bankname;
}

@end

@implementation HBInvestFailedCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)buildLayout{
    self.title = @"申请失败";
    
    UIImage *failedim = [UIImage imageNamed:@"bank_failed"];
    NSString *str = [[NSString alloc]init];
    NSString *contentstr = [[NSString alloc]init];

    
    if ([self.confirmInvestInfo.investStatus isEqualToString:@"2"]) {
        str = @"卡内余额不足!";
        contentstr = @"您可以返回修改投资金额或者更换新的银行卡进行投资,感谢您的配合!";
    }else if([self.confirmInvestInfo.investStatus isEqualToString:@"1"]){
        str = @"投资申请失败!";
        contentstr = @"由于支付通道出现问题,您的本次投资申请失败,您可以重新操作,谢谢您的配合。如有疑问请联系我们。";
    }
    
    
    CGSize strsize = [str sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:22]}];
    UIImageView *wrongvi = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2 - (failedim.size.width + 20 + strsize.width)/2, 43, failedim.size.width, failedim.size.height)];
    wrongvi.image = failedim;
    [self.view addSubview:wrongvi];
    
    UILabel *title = [self LabelWithRect:CGRectMake(wrongvi.origin.x + failedim.size.width + 20, wrongvi.origin.y + (failedim.size.height /2 - 11), strsize.width, 22) textColor:[UIColor whiteColor] textstr:str textfont:22 textstyle:NSTextAlignmentLeft];
    [self.view addSubview:title];
    
    UILabel *content = [self LabelWithRect:CGRectMake(15, wrongvi.origin.y + failedim.size.height + 45, SCREEN_WIDTH - 30, 0) textColor:[LCCommon getColor:@"b1b1b1"] textstr:contentstr textfont:14 textstyle:NSTextAlignmentLeft];
    [content sizeToFit];
    [self.view addSubview:content];
    
    NSString *kestr = @"客服电话：400-875-6009";
    UILabel *kefu = [self LabelWithRect:CGRectMake(15, content.origin.y + content.size.height + 15, SCREEN_WIDTH - 30, 14) textColor:[LCCommon getColor:@"b1b1b1"] textstr:kestr textfont:14 textstyle:NSTextAlignmentLeft];
    
    
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:kefu.text];
    [noteStr addAttribute:NSForegroundColorAttributeName value:[LCCommon getColor:@"2281f7"] range:NSMakeRange(5, 12)];
    [kefu setAttributedText:noteStr] ;
    
    
    UIView *line = [[UIView alloc]init];

    
    
    if ([self.confirmInvestInfo.investStatus isEqualToString:@"2"]) {
        
        line = [self makeLinewithRect:CGRectMake(15, content.origin.y + content.size.height + 15, SCREEN_WIDTH - 30, .5) Color:[LCCommon getColor:@"dddddd"] addview:self.view];
        [self makeLinewithRect:CGRectMake(15, line.origin.y + 45, line.width, .5) Color:[LCCommon getColor:@"dddddd"] addview:self.view];
        
    }else if([self.confirmInvestInfo.investStatus isEqualToString:@"1"]){
        [self.view addSubview:kefu];
        line = [self makeLinewithRect:CGRectMake(15, kefu.origin.y + 14 + 15, SCREEN_WIDTH - 30, .5) Color:[LCCommon getColor:@"dddddd"] addview:self.view];
        [self makeLinewithRect:CGRectMake(15, line.origin.y + 45, line.width, .5) Color:[LCCommon getColor:@"dddddd"] addview:self.view];
    }
    
    UIImage *bankim = [UIImage imageNamed:self.confirmInvestInfo.bankCode];
    _bankvi = [[UIImageView alloc]initWithFrame:CGRectMake(15, line.origin.y + (45/2 - bankim.size.height/2), bankim.size.width, bankim.size.height)];
    _bankvi.image = bankim;
    [self.view addSubview:_bankvi];
    
    _bankname = [self LabelWithRect:CGRectMake(15 + bankim.size.width + 3, line.origin.y + (45/2 - 16/2), SCREEN_WIDTH, 16) textColor:[LCCommon getColor:@"b1b1b1"] textstr:[NSString stringWithFormat:@"%@(%@)",self.confirmInvestInfo.bankName,self.confirmInvestInfo.bankCardShort] textfont:16 textstyle:NSTextAlignmentLeft];
    [self.view addSubview:_bankname];

    UIButton *bleowbtn = [[UIButton alloc]initWithFrame:CGRectMake(15, SCREEN_HEIGHT - 70 - 45 - 64, SCREEN_WIDTH - 30, 45)];
    bleowbtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [bleowbtn setBackgroundColor:[LCCommon getColor:@"41434a"] forState:UIControlStateNormal];
    [bleowbtn addTarget:self action:@selector(choseTopush:) forControlEvents:UIControlEventTouchUpInside];
    bleowbtn.clipsToBounds = YES;
    bleowbtn.layer.cornerRadius = 4;
    [self.view addSubview:bleowbtn];
    
    
    UIButton *hidebtn = [[UIButton alloc]initWithFrame:CGRectMake(15, bleowbtn.origin.y - 10 - 45, SCREEN_WIDTH - 30, 45)];
    hidebtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [hidebtn setBackgroundColor:[LCCommon getColor:@"f34546"] forState:UIControlStateNormal];
    [hidebtn setBackgroundColor:[LCCommon getColor:@"d43333"] forState:UIControlStateHighlighted];
    [hidebtn addTarget:self action:@selector(choseTopush:) forControlEvents:UIControlEventTouchUpInside];
    hidebtn.clipsToBounds = YES;
    hidebtn.layer.cornerRadius = 4;
    [self.view addSubview:hidebtn];
    
    if ([self.confirmInvestInfo.investStatus isEqualToString:@"2"]) {
        [bleowbtn setTitle:@"更换银行卡" forState:UIControlStateNormal];
        bleowbtn.tag = 1001;
        [hidebtn setTitle:@"修改投资金额" forState:UIControlStateNormal];
        hidebtn.tag = 1002;
    }else if([self.confirmInvestInfo.investStatus isEqualToString:@"1"]){
        [bleowbtn setTitle:@"查看交易记录" forState:UIControlStateNormal];
        bleowbtn.tag = 1003;
        [hidebtn setTitle:@"重新投资" forState:UIControlStateNormal];
        hidebtn.tag = 1004;
    }
}


-(void)choseTopush:(UIButton *)sender{
    UIViewController *ctrl = [[UIViewController alloc]init];
    switch (sender.tag) {
        case 1001:
            ctrl = [[HBMyBankAccountCtrl alloc]init];
            ((HBMyBankAccountCtrl*)ctrl).bindType = MyBankBindType_YES;
            [self.navigationController pushViewController:ctrl animated:YES];
            break;
            
            case 1002:
                for (UIViewController *popctrl in self.navigationController.viewControllers) {
                    if ([popctrl isKindOfClass:[HBInvestCtrl class]]) {
                        [self.navigationController popToViewController:popctrl animated:YES];
                        break;
                    }
                }
            break;
            
            case 1003:
                ctrl = [[HBBuyingRecordCtrl alloc]init];
                [self.navigationController pushViewController:ctrl animated:YES];
            break;
            
            case 1004:
            for (UIViewController *popctrl in self.navigationController.viewControllers) {
                if ([popctrl isKindOfClass:[HBInvestCtrl class]]) {
                    [self.navigationController popToViewController:popctrl animated:YES];
                    break;
                }
            }
            break;
            
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    if (self.navigationController.viewControllers[0] != self) {
        self.navigationItem.leftBarButtonItem = leftItem;
    }
}


-(UILabel *)LabelWithRect:(CGRect)rect textColor:(UIColor *)color textstr:(NSString *)str textfont:(int)font textstyle:(NSTextAlignment)aligment{
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.textColor = color;
    label.text = str;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = aligment;
    label.numberOfLines = 0;
    return label;
}

-(UIView *)makeLinewithRect:(CGRect)rect Color:(UIColor *)color addview:(UIView *)vi{
    UIView *line = [[UIView alloc]initWithFrame:rect];
    line.backgroundColor = color;
    [vi addSubview:line];
    return line;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
